<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\DashangGift;
use app\api\model\dao\DashangRecord;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\api\model\data\note\NoteData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;
use think\Validate;

class GetRewardList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteData;
    private $objNotePraise;
    private $objDashangRecond;
    private $objUserAccount;
    private $objUserCurrency;
    private $objGift;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objNoteData = new NoteData();
        $this->objNotePraise = new NotePraise();
        $this->objDashangRecond = new DashangRecord();
        $this->objUserAccount = new UserAccount();
        $this->objGift = new DashangGift();
        $this->objUserCurrency = new UserCurrency();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'note_id' => 'require',
        ];
        $msg = [
            'note_id.require' => '帖子id不能为空',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $dashang_recond = $this->objDashangRecond->getDashangRecord(['note_id' => ['=', $param['note_id']]]);
        if (empty($dashang_recond)) {
            $dashang_recond = [];
        }

        $userid = [];
        foreach ($dashang_recond as $val) {
            $userid[] = $val['userid'];
        }

        $userid2User = [];
        if (!empty($userid)) {
            $user_account_dashang = $this->objUserAccount->getUserAccountByUserids($userid, ['id', 'nick_name', 'avatar']);
            if (!empty($user_account_dashang)) {
                $user_account_dashang = $this->modelToArray($user_account_dashang);
                foreach ($user_account_dashang as $val) {
                    $userid2User[$val['id']] = $val;
                }
            }
        }

        $data = [];
        foreach ($dashang_recond as $val) {
            $data[] = [
                'userid' => $val['userid'],
                'name' => $userid2User[$val['userid']]['nick_name'],
                'photo' => $userid2User[$val['userid']]['avatar'],
                'money' => strval($val['money']),
                'gift_name' => $val['gift_name'],
                'popularity' => strval($val['popularity']),
            ];
        }

        $this->data['list'] = $data;
        $this->data['total'] = count($data);
        $this->data['mine_money'] = '0';

        // 获取我的漫币数
        $user_currency = $this->objUserCurrency->getCurrencyByUserid($this->user_id, Flag::CURRENCY_TYPE_COIN);
        if (false === $user_currency) {
            Log::error(__METHOD__ . ' get user_currency fail.');
            return $this->getRes(Error::ERR_SYS);
        }

        if (!empty($user_currency)) {
            $this->data['mine_money'] = strval($user_currency['amount']);
        }

        return $this->getRes();
    }
}
